1 /*-------------------------------------------------------------------------------------------------------*\
2 | Adium, Copyright (C) 2001-2005, Adam Iser (adamiser@mac.com | http://www.adiumx.com) |
3 \---------------------------------------------------------------------------------------------------------/
4 | This program is free software; you can redistribute it and/or modify it under the terms of the GNU
5 | General Public License as published by the Free Software Foundation; either version 2 of the License,
6 | or (at your option) any later version.
8 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
9 | the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
10 | Public License for more details.
12 | You should have received a copy of the GNU General Public License along with this program; if not,
13 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
14 \------------------------------------------------------------------------------------------------------ */
16 #import "AIToolbarUtilities.h"
18 @implementation AIToolbarUtilities
20 + (void)addToolbarItemToDictionary:(NSMutableDictionary *)theDict withIdentifier:(NSString *)identifier label:(NSString *)label paletteLabel:(NSString *)paletteLabel toolTip:(NSString *)toolTip target:(id)target settingSelector:(SEL)settingSelector itemContent:(id)itemContent action:(SEL)action menu:(NSMenu *)menu
22 NSToolbarItem *item = [self toolbarItemWithIdentifier:identifier label:label paletteLabel:paletteLabel toolTip:toolTip target:target settingSelector:settingSelector itemContent:itemContent action:action menu:menu];
24 [theDict setObject:item forKey:identifier];
27 + (NSToolbarItem *)toolbarItemWithIdentifier:(NSString *)identifier label:(NSString *)label paletteLabel:(NSString *)paletteLabel toolTip:(NSString *)toolTip target:(id)target settingSelector:(SEL)settingSelector itemContent:(id)itemContent action:(SEL)action menu:(NSMenu *)menu
29 NSToolbarItem *item = [[[NSToolbarItem alloc] initWithItemIdentifier:identifier] autorelease];
32 [item setLabel:label];
33 [item setPaletteLabel:paletteLabel];
34 [item setToolTip:toolTip];
37 [item setTarget:target];
40 /* The settingSelector parameter can either be @selector(setView:) or @selector(setImage:). Pass in the right
41 * one depending upon whether your NSToolbarItem will have a custom view or an image, respectively
42 * (in the itemContent parameter). Then this next line will do the right thing automatically.
44 if (settingSelector && itemContent) {
45 [item performSelector:settingSelector withObject:itemContent];
48 [item setAction:action];
51 /* If this NSToolbarItem is supposed to have a menu "form representation" associated with it (for text-only mode),
52 * we set it up here. Actually, you have to hand an NSMenuItem (not a complete NSMenu) to the toolbar item,
53 * so we create a dummy NSMenuItem that has our real menu as a submenu.
56 //We actually need an NSMenuItem here, so we construct one
57 mItem = [[[NSMenuItem alloc] init] autorelease];
58 [mItem setSubmenu: menu];
59 [mItem setTitle: [menu title]];
60 [item setMenuFormRepresentation:mItem];
66 + (NSToolbarItem *)toolbarItemFromDictionary:(NSDictionary *)theDict withIdentifier:(NSString *)itemIdentifier
69 NSToolbarItem *newItem;
71 item = [theDict objectForKey:itemIdentifier];
72 newItem = [[item copy] autorelease];
74 if ([item view] != NULL) {
75 if ([[item view] respondsToSelector:@selector(copyWithZone:)]) {
76 [newItem setView:[[[item view] copy] autorelease]];
79 /* For a toolbar only used in one window at a time, it's alright for a view to not allow copying.
80 * If the view doesn't conform to NSCopying, use the _same_ view. NSToolbar's copy method will have created a new NSView
81 * and attempted to make it match to the original one.
83 [newItem setView:[item view]];
87 //If we have a custom view, we *have* to set the min/max size - otherwise, it'll default to 0,0 and the custom
88 //view won't show up at all! This doesn't affect toolbar items with images, however.
89 if ([newItem view] != NULL) {
90 [newItem setMinSize:[item minSize]];
91 [newItem setMaxSize:[item maxSize]];
93 if ([[newItem view] respondsToSelector:@selector(setToolbarItem:)]) {
94 [[newItem view] setToolbarItem:newItem];